﻿using System.Text;

namespace Utilities
{
    public class IndentedBuilder
    {
        public StringBuilder StringBuilder 
        { 
            get { return _builder; } 
        }

        /// <summary>
        /// The string to be used to indent
        /// </summary>
        public string IndentString { get; private set; }

        /// <summary>
        /// The indent level
        /// </summary>
        public int Indent { get; set; }

        public IndentedBuilder(string indentString = "    ")
        {
            IndentString = indentString;
        }

        public void Append(string s)
        {
            _builder.Append(s);
        }

        public void AppendLine(string s)
        {
            _builder.AppendLine(s);
        }

        public void AppendLine()
        {
            _builder.AppendLine();
        }

        public void AppendIndent(string s)
        {
            AppendIndent();
            _builder.Append(s);
        }

        public void AppendLineIndent(string s)
        {
            AppendIndent();
            _builder.AppendLine(s);
        }

        public void AppendIndent()
        {
            for (int i = 0; i < Indent; ++i)
            {
                _builder.Append(IndentString);
            }
        }

        /// <summary>
        /// Returns the string from the buffer and clears the string buffer
        /// </summary>
        /// <returns></returns>
        public string ToString(bool clear = false)
        {
            string s = _builder.ToString();

            if (clear)
            {
                _builder.Clear();
            }
            return s;
        }

        /// <summary>
        /// The string builder used by the indented builder
        /// </summary>
        private StringBuilder _builder = new StringBuilder();
    }
}
